//
//  FileRouter.swift
//  soapp_Example
//
//  Created by zhuxietong on 2019/4/27.
//  Copyright © 2019 CocoaPods. All rights reserved.
//


import Foundation

public enum FileRouter:AppRouter {
    case video
    case audio
    case image
    case unkown
    
    public static var image_dots:[String] = ["png","jpg","jpeg"]
    public static var audio_dots:[String] = ["mp3"]
    public static var video_dots:[String] = ["mp4"]

    
    public static func dot(url:String)->String {
        var file_extention = ""
        let reg = Regex(".+\\.([\\w]+)(\\W.*)?$")
        if let subject = reg.firstMatch(in: url)?.captures[0] {
            file_extention = subject
        }
        return file_extention.lowercased()
    }
    
    public static func type(url:String)->FileRouter?{
        let _dot = FileRouter.dot(url: url)
        if image_dots.contains(_dot){
            return .image
        }
        if video_dots.contains(_dot){
            
            return .video
        }
        if audio_dots.contains(_dot){
            
//            return __isCurrent
            return .audio
        }
        
        return nil
    }

    
    public static func open(link: String, at: UIViewController?, present: NavPresentType?) -> Bool {
        
        let info = AppLink.paw(link: link)
        if info.path == DownloadTasksCtr.Link{
            let ctr = DownloadTasksCtr()
            ctr.isTest = false
            return AppLink.show(at: at, controller: ctr, present: NavPresentType.typeWith(url: link))
        }
        
        let model = info.params.mutable_dictionary
        guard let router = FileRouter.type(url: info.path) else {
            return false
        }
        
//        if link.hasPrefix("file://")
//        {
        
            
            switch router{
            case .video:

                let ctr = EeVideoPlayer()
                model["url"] = info.path
                ctr.mo.model = model
                return AppLink.show(at: at, controller: ctr, present: NavPresentType.typeWith(url: link))
            case .image:
                break
            case .audio:
                var isCurrent = false
                if let _url = EeAudioPlayer.shared.jukebox.currentItem?.URL.path
                {
                    if _url == info.path
                    {
                        isCurrent = true
                    }
                }
                let item:EeAudioItem = EeAudioItem(url: info.path, cover: model["cover",""], name: model["name",""])
                if isCurrent
                {
                    
                    if EeAudioPlayer.shared.currentPlayPercentage >= 1{
                        EeAudioPlayer.shared.replayAction()
                        EeAudioControl.showControl()
                        EeAudioPlayer.shared.jukebox.play()
                    }
                    else{
                        EeAudioControl.showControl()
                        EeAudioPlayer.shared.jukebox.play()
                    }
                }
                else{
                    let _ = EeAudioManager.shared.add(item)
                    co_delay(delay: 0.1) {
                        EeAudioPlayer.shared.play(item: item)
                        EeAudioControl.showControl()
                    }
                }
                
//                else{
//                    item.url = mo.model["file_url",""]
//                    item.cover = mo.model["thumb",""]
//                    let _ = EeAudioManager.shared.add(item)
//                    mo.model["needLoad"] = "NO"
//                    weak var wself = self
//                    co_delay(delay: 0.1) {
//                        guard let ws = wself else{
//                            return
//                        }
//
//                        EeAudioPlayer.shared.play(item: ws.item)
//                    }
//
                
                    //            mo.model["needLoad"] = "NO"
//                }

                

            case .unkown:
                break
            }
//        }
        
        return false
    }
}
